<template>
  <div class="profile-task">
    <div class="header fillinfo-modal">
      <a-form>
        <a-form-item :label="$t('label.time_filter')">
          <a-range-picker
            :show-time="{ format: 'HH:mm:ss' }"
            format="YYYY-MM-DD HH:mm:ss"
            :placeholder="['Start Time', 'End Time']"
            @change="onRangeChange"
          />
        </a-form-item>
        <a-form-item :label="$t('label.search')" class="flex-1">
          <div class="search-item">
            <a-input
              :placeholder="$t('placeholder.profile_to_order_search')"
              v-model:value="serachForm.keyword"
            ></a-input>
            <a-button
              style="background: #13c29c"
              type="primary"
              @click="onClicksearchBtn"
              >{{ $t("btnText.search_query") }}</a-button
            >
          </div>
        </a-form-item>
      </a-form>
    </div>
    <div class="pc-data">
      <div class="table">
        <div class="table-header">
          <div class="table-header-item col-index">
            {{ $t("table.profile_task.col_1") }}
          </div>
          <div class="table-header-item col-id">
            {{ $t("table.profile_task.col_id") }}
          </div>
          <div class="table-header-item col-5">
            {{ $t("table.profile_task.col_2") }}
          </div>
          <div class="table-header-item flex-1">
            {{ $t("table.profile_task.col_3") }}
          </div>
          <div class="table-header-item col-5">
            {{ $t("table.profile_task.col_5") }}
          </div>
          <div class="table-header-item col-task-type">
            {{ $t("table.profile_task.col_task_type") }}
          </div>
          <div class="table-header-item col-5">
            {{ $t("table.profile_task.col_task_status") }}
          </div>
          <div class="table-header-item col-4">
            {{ $t("table.profile_task.col_4") }}
          </div>
          <div class="table-header-item col-7">
            {{ $t("table.profile_task.col_7") }}
          </div>
        </div>
        <div class="table-body" v-if="TaskData">
          <template v-for="(item, index) in TaskData.data" :key="item.id">
            <div class="table-row">
              <div class="table-row-item col-index">
                {{ parseTableId(index + 1) }}
              </div>
              <div class="table-row-item col-id">{{ item.task_no }}</div>
              <div class="table-row-item col-5">
                {{ item.member_last_name + ' ' + item.member_first_name }}
              </div>
              <div class="table-row-item flex-1" style="word-break: break-all">
                {{ item.title }}
              </div>

              <div class="table-row-item col-5">
                {{
                  item.receive_last_name
                    ? item.receive_last_name +' '+ item.receive_first_name
                    : $t("tag.none")
                }}
              </div>
              <div class="table-row-item col-task-type">
                {{ $t("tag.user") }}
              </div>
              <div class="table-row-item status col-5">
                <span
                  :class="
                    item.status == '1'
                      ? 'pre-pay'
                      : item.status == '3'
                      ? 'payed'
                      : item.status == '4'
                      ? 'complete'
                      : 'pre-pay'
                  "
                  >{{ item.status_title }}</span
                >
              </div>
              <div class="table-row-item col-4">{{ item.create_time }}</div>
              <div class="table-row-item col-7 table-row-col-end">
                <div class="btn" @click="getTaskDetailData(item.id)">
                  <IconDetail />
                  <span>{{ $t("btnText.detail") }}</span>
                </div>
                <span v-if="!item.receive_last_name">|</span>
                <div
                  class="btn"
                  @click="allocateBtn(item)"
                  v-if="!item.receive_last_name"
                >
                  <IconAllocate />
                  <span>{{ $t("btnText.allocate") }}</span>
                </div>
              </div>
            </div>
          </template>
        </div>
        <template v-else>
          <div class="pc-skeleton">
            <Skeleton height="30px" block v-for="i in 8" :key="i"></Skeleton>
          </div>
        </template>
      </div>
    </div>
    <div class="mobile-data">
      <div class="mobile-task-list" v-if="TaskData">
        <div
          class="mobile-task-item"
          v-for="item in TaskData.data"
          :key="item.id"
        >
          <div class="task-header">
            <div class="title">
              {{ item.title }}
            </div>
            <div class="status">
              <span
                :class="
                  item.status == '1'
                    ? 'pre-pay'
                    : item.status == '3'
                    ? 'payed'
                    : item.status == '4'
                    ? 'complete'
                    : 'pre-pay'
                "
                >{{ item.status_title }}</span
              >
            </div>
          </div>
          <div class="detail">
            <div class="publisher">
              <div class="label">{{ $t("table.profile_task.col_2") }}</div>
              <div class="value">
                {{ item.member_last_name + ' ' + item.member_first_name }}
              </div>
            </div>
            <div class="receiver">
              <div class="label">{{ $t("table.profile_task.col_5") }}</div>
              <div class="value">
                {{ item.receive_last_name +' '+ item.receive_first_name }}
              </div>
            </div>
            <div class="time">
              <div class="label">{{ $t("table.profile_task.col_4") }}</div>
              <div class="value">
                {{ item.create_time.split(" ")[0] }}
              </div>
            </div>
          </div>
          <div class="divider-line"></div>
          <div class="footer">
            <div class="btn" @click="getTaskDetailData(item.id)">
              <IconDetail />
              <span>{{ $t("btnText.detail") }}</span>
            </div>
            <span v-if="true" style="visibility: hidden" class="split">|</span>
            <div class="btn" @click="allocateBtn(item)">
              <IconAllocate />
              <span>{{ $t("btnText.allocate") }}</span>
            </div>
          </div>
        </div>
      </div>
      <template v-else>
        <div class="mobile-skeleton">
          <div class="mobile-skeleton-inner">
            <div class="mobile_inner" v-for="i in 4" :key="i">
              <Skeleton width="40%" mobile></Skeleton>
              <Skeleton :rows="2" mobile></Skeleton>
              <div class="footer_btns">
                <Skeleton width="100%" mobile></Skeleton>
                <Skeleton width="100%" mobile></Skeleton>
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>

  <!-- 弹窗 -->
  <!-- 任务详情 -->
  <DialogTaskDetail ref="dialogTaskDetailRef" :id="task_id" />
  <!-- 任务分配 -->
  <DialogTaskToOrder
    ref="dialogTaskToOrderRef"
    @allocate-success="getProfileTaskData"
    :list="orderUserList"
    :id="task_id"
    :task_no="task_no"
  />
</template>

<script setup lang="ts">
import { Modal, message } from "ant-design-vue";
import {
  h,
  onBeforeMount,
  nextTick,
  ref,
  watch,
  getCurrentInstance,
  onMounted,
} from "vue";
import {
  postCreateTask,
  postMemberTaskList,
  postTaskCommentList,
  postTaskCommentReplay,
  postTaskDetail,
  postTaskFollowUpList,
  postTaskOrderList,
} from "../../../api/task";
import { postUploadSingleImg } from "../../../api/api";
import type { PageResult } from "../../../types/global";
import type { TaskCreateParams, TaskItem } from "../../../types/task";
import { RegLink } from "../../../data/reg";
import { useMemberStore } from "../../../store";

const memberStore = useMemberStore();

const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;

const parseTableId = computed(() => {
  return (index: number) => {
    if (index < 10) {
      return `00${index}`;
    }
    if (index < 100) {
      return `0${index}`;
    }
    return index;
  };
});
//  header  =========================================================
const navList = ref([
  {
    id: 1,
    label: "待审",
  },
  {
    id: 2,
    label: "已审核",
  },
  {
    id: 3,
    label: "抢单申请",
  },
  {
    id: 4,
    label: "系统分配",
  },
]);

onBeforeMount(() => {
  setTimeout(() => {
    setNavList();
  });
});

const setNavList = () => {
  navList.value[0].label = $t("content.profile.task.navList.label_1");
  navList.value[1].label = $t("content.profile.task.navList.label_2");
  navList.value[2].label = $t("content.profile.task.navList.label_3");
  navList.value[3].label = $t("content.profile.task.navList.label_4");
};

const navActiveIndex = ref(0);
watch(navActiveIndex, (n) => {
  getProfileTaskData();
});

// =============任务详情 =================
const dialogTaskDetailRef = ref();
const getTaskDetailData = async (id: number) => {
  if (isMobile()) {
    goto("/mobile_detail/task", { id });
    return;
  }
  task_id.value = id;
  nextTick(() => {
    dialogTaskDetailRef.value.showDialog();
  });
};

const onRangeChange = (value: any, dateString: [string, string]) => {
  serachForm.value.time_range = dateString;
};

const TaskData = ref<PageResult<TaskItem[]>>();
let cacheTaskData: any = null;
const getProfileTaskData = async () => {
  TaskData.value = undefined;
  let res = await postMemberTaskList(2);
  if (res.code === 200) {
    TaskData.value = res.data;
    cacheTaskData = JSON.parse(JSON.stringify(res.data));
  }
};

onBeforeMount(() => {
  getProfileTaskData();
});

const task_id = ref();
// 查询数据
const serachForm = ref<{
  keyword: string;
  time_range: [string, string] | [];
}>({
  keyword: "",
  time_range: [],
});
const onClicksearchBtn = () => {
  let keyword = serachForm.value.keyword;
  let time_filter = serachForm.value.time_range[0] ? true : false;
  const filterDataClone = JSON.parse(JSON.stringify(cacheTaskData.data));
  TaskData.value!.data = filterDataClone.filter((item) => {
    let publisher_name = item.member_last_name +' '+ item.member_first_name;
    let receiver_name = item.receive_last_name +' '+ item.receive_first_name;
    let date_range = true;
    if (time_filter) {
      date_range =
        new Date(serachForm.value.time_range[0]!).getTime() <=
          new Date(item.create_time).getTime() &&
        new Date(item.create_time).getTime() <=
          new Date(serachForm.value.time_range[1]!).getTime();
    }
    if (
      (publisher_name.includes(keyword) || receiver_name.includes(keyword)) &&
      date_range
    )
      return item;
  });
};

const dialogTaskToOrderRef = ref();

const task_no = ref();
const orderUserList = ref();
const allocateBtn = async (item: TaskItem) => {
  let taskId = item.id;
  // 有数据且id相同直接弹窗
  if (
    orderUserList.value &&
    orderUserList.value.length > 0 &&
    taskId == task_id.value
  ) {
    if (isMobile()) {
      sessionStorage.setItem("taskDetailData", JSON.stringify(item));
      goto("/mobile/allocate");
      return;
    }
    dialogTaskToOrderRef.value.showDialog();
    return;
  }
  // 判断是否有分配的人员
  let res = await postTaskOrderList(taskId);
  task_id.value = taskId;
  task_no.value = item.task_no;
  // 无分配人员
  orderUserList.value = res.data || [];
  if (orderUserList.value.length == 0) {
    message.error("暂无可分配人员");
    return;
  }
  if (isMobile()) {
    sessionStorage.setItem("taskDetailData", JSON.stringify(item));
    goto("/mobile/allocate");
    return;
  }
  // 有分配人员
  nextTick(() => {
    dialogTaskToOrderRef.value.showDialog();
  });
};
</script>

<style scoped lang="less">
@import url("./common.less");
.pc-skeleton {
  display: flex;
  flex-direction: column;
  & > div {
    margin-top: 30px;
  }
}
.mobile-skeleton {
  .mobile-skeleton-inner {
    display: flex;
    flex-direction: column;
    gap: 123px;
  }
  .mobile_inner {
    display: flex;
    flex-direction: column;
    gap: 61px;
    .footer_btns {
      display: flex;
      gap: 61px;
    }
  }
}

.header {
  margin-bottom: 0;
  :deep(.ant-form) {
    display: flex;
    gap: 50px;
    .search-item {
      display: flex;
      gap: 30px;
      .ant-input {
        min-width: 100px;
      }
    }
    .ant-picker {
      input {
        @media screen and (min-width: 768px) {
          font-size: 18px !important;
        }
      }
    }
    .ant-form-item {
      margin-bottom: 24px;
    }
    .ant-form-item-label {
      display: flex;
      align-items: center;
    }
    .ant-form-item.flex-1 {
      max-width: 400px;
    }
  }
  .nav {
    height: 25px;
  }
  .ant-btn {
    height: 40px !important;
    border-radius: 12px !important;
    font-size: 14px !important;
  }
}
.mobile-data {
  display: none;
}

@media screen and (max-width: 768px) {
  .fillinfo-modal {
    :deep(.ant-form) {
      flex-direction: column;
      .ant-form-item {
        max-width: 100% !important;
      }
      .ant-form-item-label label {
        font-size: 72px;
      }
      .ant-form-item-control-input {
        .ant-form-item-control-input-content,
        .ant-picker {
          height: 100%;
        }
      }
      input {
        font-size: 72px !important;
      }
      .search-item {
        gap: 82px;
        align-items: center;
        input {
          flex: 1;
        }
        .ant-btn {
          height: 100% !important;
        }
      }
    }
  }
  .mobile-data {
    display: block;
    .add-new-task {
      position: fixed;
      bottom: 300px;
      right: 200px;
      z-index: 9;
      // background-color: #fff;
      border-radius: 50%;
      .svg-icon {
        width: 200px;
        height: 200px;
      }
    }
  }
  .pc-data {
    display: none;
  }
  .header {
    margin-bottom: 123px;
    .ant-btn {
      height: 205px !important;
      padding: 51px 123px !important;
      border-radius: 61px !important;
      font-size: 72px !important;
    }
  }
}
</style>
